package apps.business.process.sales.contract;

import java.util.ArrayList;
import java.util.List;

import jpa.entitys.synchro.staging.SdCustoRequDeducOutb;

import org.apache.log4j.Logger;

import promis.utility.CommonService;

import sap.business.sd.outbound.VwSyncMktBankDeduRequService;

import apps.business.process.sales.booking.SalesBookingProcessing;
import apps.business.process.sales.booking.SalesBookingSingleProcessing;
import apps.promis.out.sales.VwSyncMktBankDeduRequAction;
import apps.promis.out.sales.VwSyncMktBookConstructionAction;

public class SalesContractProcessing {
	
	private static final Logger log = Logger.getLogger(SalesContractProcessing.class);
	
	
	/**
	 * 		String company = "SP";
			String project = "SPLVILLE-PRAGSA";  //SPLVILLE-PRAGSA
			String BookingOrContarctNo = "CS-1206-000022-0"; //  "CS-1204-000009-0" // "BS-1204-000007"; //BS-1204-000016
			String ReceipteddocumentNo = "RR-1206-000066";
			String status = "A";
			String syncIsstaging = "N"; //Can set value to [null]
			String issync = "N";
	 * @param company
	 * @param project
	 * @param BookingOrContarctNo
	 * @param ReceipteddocumentNo
	 * @param status
	 * @param syncIsstaging
	 * @param issync
	 */
	public void Processing(String company, String project, String BookingOrContarctNo, String ReceipteddocumentNo, String status, String syncIsstaging, String issync){
		try{
			
			new SalesBookingProcessing().newProcessing(company, project, BookingOrContarctNo, ReceipteddocumentNo, status, null, syncIsstaging, issync);
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
	public void ProcessingAll() throws Exception {
    	try{
    		
    		try{
    			String company = "SP";
    			
    			List<String> lst = new ArrayList<String>();
    			lst.add("BY");
    			lst.add("CGVA");
    			lst.add("CH3A1");
    			lst.add("CH3A2");
    			lst.add("CH3A3");
    			lst.add("CH3A4");
    			lst.add("CH3A5");
    			lst.add("CH3A6");
    			lst.add("CH3CT");
    			lst.add("CH4");
    			lst.add("CHR");
    			lst.add("CHS");
    			lst.add("CRF1");
    			lst.add("CRF2");
    			lst.add("CRRH");
    			lst.add("CRV1");
    			lst.add("CRV2");
    			lst.add("EX");
    			lst.add("NVOL");
    			lst.add("OP3-3");
    			lst.add("PO");
    			lst.add("PT");
    			lst.add("PVD");
    			lst.add("RP");
    			lst.add("SCH");
    			lst.add("SCR-C");
    			lst.add("SCR-R");
    			lst.add("SCR-T");
    			lst.add("SGV");
    			lst.add("SGV-L");
    			lst.add("SGV-P");
    			lst.add("SGV5");
    			lst.add("SGVJL");
    			lst.add("SGVP");
    			lst.add("SH");
    			lst.add("SNVTP");
    			lst.add("SP-AP");
    			lst.add("SP-DR");
    			lst.add("SP-S");
    			lst.add("SPET");
    			lst.add("SPK-A");
    			lst.add("SPK-K");
    			lst.add("SPK-P");
    			lst.add("SPK-R");
    			lst.add("SPK-T");
    			lst.add("SPKRP");
    			lst.add("SPKW");
    			lst.add("SPP-S");
    			lst.add("SPPC");
    			lst.add("SPR");
    			lst.add("SPV-P");
    			lst.add("SPVOR");
    			lst.add("SPVR5");
    			lst.add("SPVRS");
    			lst.add("SRP");
    			lst.add("SRS");
    			lst.add("SSL");
    			lst.add("SV-PC");
    			lst.add("SV-RB");
    			lst.add("SV-SW");
    			lst.add("SV5");
    			lst.add("SVO-L");
    			lst.add("SVP");
    			lst.add("SVP-N");
    			lst.add("SVP52");
    			lst.add("SVSP");
    			lst.add("SVT");
    			lst.add("SW");
    			lst.add("VCM");
    			lst.add("VP");
    			lst.add("VP48");
    			lst.add("VPS");
    			lst.add("VRAM");
    			lst.add("VRK");
    			lst.add("VSK");
    			lst.add("VSP");
    			
    			String docType = "CS";
    			
    			for(String project : lst){
    				
    				List<Object[]> lst1 = new VwSyncMktBookConstructionAction().findAllByProject(company, project, docType);
    				if(lst != null){
    					for(Object[] value : lst1 ){
    						
    						String BookingOrContarctNo = CommonService.IsNullRNull(value[2]);
    						String status = CommonService.IsNullRNull(value[3]);
    						String syncIsstaging = CommonService.IsNullRNull(value[4]);
    						String issync = "N";
    						
    					    new SalesBookingSingleProcessing().Processing(company, project, BookingOrContarctNo, status, syncIsstaging, issync);
    					    
    					}
    				}
    			}
    			
    		}catch(Exception e){
    			log.error(e.getMessage());
    			System.err.println(e.getMessage());
    		}
    		
    	}catch(Exception e){
    		log.error(e.getMessage());
			System.err.println(e.getMessage());
    		throw e;
    	}
    }
	
	/**
	 * company, project, document_no, REF_CONTRACT_NO, status ,SYNC_ISSTAGING
	 * @throws Exception
	 */
	private List<Object[]> SearchStagingIsNotN() throws Exception {
		try{
			
			StringBuilder _strQuery = new StringBuilder();			  		
			
//			_strQuery.append(" Select ");  
//			_strQuery.append(" Company, Project, Document_no, REF_CONTRACT_NO, status ,SYNC_ISSTAGING, CREATED_BY "); 
//			_strQuery.append(" From VW_SYNC_RCVD_INSTALLMENT ");
//			_strQuery.append(" Where STATUS IN ( 'A' ,'Z')  ");
//			_strQuery.append(" AND SYNC_ISSTAGING = 'N'  ");
//			_strQuery.append(" AND RECEIPT_TYPE <> 'RX' ");
//			_strQuery.append(" AND REF_CONTRACT_NO IS NULL ");
//			_strQuery.append(" Group by company, project, document_no, REF_CONTRACT_NO, status, SYNC_ISSTAGING ,CREATED_BY ");
//			_strQuery.append(" Order by company, project, document_no, REF_CONTRACT_NO, status, SYNC_ISSTAGING ,CREATED_BY ");
			
			_strQuery.append(" SELECT COMPANY, PROJECT, DOCUMENT_NO, REF_CONTRACT_NO, STATUS ,SYNC_ISSTAGING, CREATED_BY  ");
			_strQuery.append(" FROM SPL.VW_SYNC_RECEIPT_ISNOT_SYNC WHERE NVL(CREATED_BY,'-') <> 'RMS' ");
			_strQuery.append(" ORDER BY COMPANY, PROJECT, DOCUMENT_NO, REF_CONTRACT_NO, STATUS ,SYNC_ISSTAGING, CREATED_BY ");
			
			List<Object[]> list = (List<Object[]>)new VwSyncMktBookConstructionAction().getEmPromis().createNativeQuery(_strQuery.toString()).getResultList();
			if(CommonService.getSize(list) > 0){
				for(Object[] value : list){

					
					String company = CommonService.IsNullRNull(value[0]); 
					String project = CommonService.IsNullRNull(value[1]);
					String BookingOrContarctNo = CommonService.IsNullRNull(value[3]); 	//"CS-1206-000022-0";
					String ReceipteddocumentNo = CommonService.IsNullRNull(value[2]); 	//"RR-1206-000066";
					String receipt_status = CommonService.IsNullRNull(value[4]); 		//"A";
					String syncIsstaging_opt =  CommonService.IsNullRNull(value[5]);		//"N"; //Can set value to [null]
					String issync = "N";
					System.out.println("company=" + company + ", project=" + project + ", BookingOrContarctNo=" + BookingOrContarctNo +", ReceipteddocumentNo="+ ReceipteddocumentNo +", receipt_status="+ receipt_status + ", syncIsstaging_opt=" + syncIsstaging_opt);
					log.info("company=" + company + ", project=" + project + ", BookingOrContarctNo=" + BookingOrContarctNo +", ReceipteddocumentNo="+ ReceipteddocumentNo +", receipt_status="+ receipt_status + ", syncIsstaging_opt=" + syncIsstaging_opt);
					
					new SalesBookingProcessing().newProcessing(company, project, BookingOrContarctNo, ReceipteddocumentNo, receipt_status, null, syncIsstaging_opt, issync);
					
				}
			}
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
		}
		return null;
	}
	
	@Deprecated
	/**
	 * Sales, Customer, Receipted, Unit send to SAP
	 */
	private void Processing(){
		try{
			
			SalesBookingProcessing obj = new SalesBookingProcessing();
//			obj.company = "SP";
//			obj.project = "SPLCITY-SRI";
//			obj.BookingOrContarctNo = "CS-1205-000005-0";
//			obj.status = "A";
//			obj.syncIsstaging = "N"; //Can set value to [null]
//			obj.documentType = "CS"; //CS
//			obj.issync = "N";
//			obj.buildingPhase = null;
//			obj.floorZone  = null;
//			obj.unitNumber  = null;
//			obj.instanceNo = null;
			
			String company = "SP";
			String project = "SPLVILLE-PRAGSA";  //SPLVILLE-PRAGSA
			String BookingOrContarctNo = "CS-1206-000022-0"; //  "CS-1204-000009-0" // "BS-1204-000007"; //BS-1204-000016
			String ReceipteddocumentNo = "RR-1206-000066";
			String receipt_status = "A";
			String syncIsstaging = "N"; //Can set value to [null]
			String issync = "N";
			
			obj.newProcessing(company, project, BookingOrContarctNo, ReceipteddocumentNo, receipt_status, null, syncIsstaging, issync);
			
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
	public static void main(String [] args){
		try{
			
			new SalesContractProcessing().SearchStagingIsNotN();
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e);
		}
	}

}
